package com.zhsm.fuxi.app

import android.view.View
import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.map.TextureMapView
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.hankkin.library.http.HttpConfig
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.squareup.leakcanary.LeakCanary
import com.zhsm.fuxi.R
import com.zhsm.paipaiout.baselibrary.common.BaseApplication
import com.zhsm.paipaiout.baselibrary.utils.AppUtils
import com.zhsm.paipaiout.baselibrary.utils.FileUtils
import com.zhsm.paipaiout.baselibrary.utils.SPUtils
import com.zhsm.fuxi.mapmanager.MapLoader

/**
 * Created by GuZhC on 2018/9/28.
 * describe:
 */
class MyApplication : BaseApplication() {

    companion object {
        private var instance: MyApplication? = null

        fun instance() = instance!!
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        FileUtils.initSd(AppUtils.getAppName(this)!!)
        SPUtils.init(this,Constant.COMMON.SP_NAME)
        initCommon()
        initHttp()
        setRfreshLayout()
        initLeakCanary()
        //baidu map
        SDKInitializer.initialize(this)
        SDKInitializer.setCoordType(CoordType.GCJ02)
    }

    private fun initHttp(){
        HttpConfig.setCookie(PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(this)))
    }

    private fun initCommon(){
        //初始化地图加载策略,必须在所有地图操作之前
        MapLoader.getInstance().init()
        //初始化定位
        MapLoader.getInstance().initLocation(instance)

        /**
         * 第二个参数：如果发现滑动返回后立即触摸界面时应用崩溃，请把该界面里比较特殊的 View 的 class 添加到该集合中，目前在库中已经添加了 WebView 和 SurfaceView
         */
       var problemViewClassList: List<Class<out View>> = arrayListOf(TextureMapView::class.java)
        BGASwipeBackHelper.init(this,problemViewClassList)
    }

    /**
     * 刷新控件全局设置
     */
    private fun setRfreshLayout() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
//            layout.setPrimaryColorsId(R.color.white, R.color.text_black)//全局设置主题颜色
            ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate)//指定为经典Header，默认是 贝塞尔雷达Header
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f).setSpinnerStyle(SpinnerStyle.Translate)
        }
    }
    private fun initLeakCanary(){
        if (LeakCanary.isInAnalyzerProcess(this)){
            return
        }
        LeakCanary.install(this)
    }
}